package thong.servlet;

import java.io.IOException;
import java.util.SortedMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.sql.Result;

import thong.util.JdbcUtil;

/**
 * Servlet implementation class CustomerCRUDServlet
 */
@WebServlet("/Customer.do")
public class CustomerCRUDServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerCRUDServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		/*
		 *  nhận tham số từ form nhập
		 */
		String id = request.getParameter("txtId");
		String name = request.getParameter("txtFullName");
		String Address = request.getParameter("txtAddress");
        String Password = request.getParameter("txtPassword");
        String Email = request.getParameter("txtEmail");
        String Activated = request.getParameter("rdoActived");
        /* 
         * Làm việc với CSDL khi tồn tài các tham số điều khiển tương 
         * ứng (btnInsert, btnUpdate, btnDelete, btnSelect, btnSearch)
         */
        try{
	        if(request.getParameter("btnInsert") != null){
		        String sql = "INSERT INTO Customers(Id,Password, FullName, Address,Email,Activated) VALUES(?, ?, ?, ?, ?,?)";
		        JdbcUtil.executeUpdate(sql,id,Password, name, Address,Email,Activated);
		        request.setAttribute("errors", "Inserted successfully !");
	        }
	        else if(request.getParameter("btnUpdate") != null){
	        	String sql = "UPDATE Customers SET Password=?, FullName=?, Address=?,Email=?,Activated=? WHERE Id=?";
		        JdbcUtil.executeUpdate(sql,Password, name, Address, Email,Activated, id);
		        request.setAttribute("errors", "Updated successfully !");
	        }
	        else if(request.getParameter("btnDelete") != null){
		        String sql = "DELETE FROM Customers WHERE Id=?";
		        JdbcUtil.executeUpdate(sql, id);
		        request.setAttribute("errors", "Deleted successfully !");
	        }
	        //xu ly edit
	        else if(request.getParameter("lnkEdit") != null){
		        String sql = "SELECT * FROM Customers WHERE Id=?";
		        /*
		         * Truy vấn và chia sẻ kết quả trong request
		         */
		        //String id1=request.getParameter("lnkEdit");
		        Result result = JdbcUtil.toResult(sql, id);
		        @SuppressWarnings("rawtypes")
				SortedMap customer = result.getRows()[0];
		        request.setAttribute("cus", customer);
		        request.setAttribute("errors", "Loaded successfully !");
	        }
        }
        catch (Exception e) {
        	request.setAttribute("errors", "Data manipilation error !");
		}
        /*
         * Duy trì chuỗi tìm kiếm của câu lệnh sql trong session
         */
        HttpSession session = request.getSession();
        if(request.getParameter("btnSearch1") != null){
        	session.setAttribute("search", request.getParameter("txtSearch"));
        }
        else if(session.getAttribute("search") == null){
        	session.setAttribute("search", "");
        }
        String search = (String)session.getAttribute("search");
        
        /*
         * Tìm kiếm theo tên và chia sẽ kết quả trong request
         */
        String searchSql = "SELECT * FROM Customers  WHERE FullName LIKE ?";
        Result cus = JdbcUtil.toResult(searchSql, "%" + search + "%");
        request.setAttribute("cuss", cus);
        /*
         * Chuyển tiếp sang trang CustomerCRUD.jsp
         */
       // request.getRequestDispatcher("CustomerCRUD.jsp").forward(request, response);
        request.getRequestDispatcher("pages/ManagerCustomer.jsp").forward(request, response);
	}

}
